package org.example.Ollama.client.service.impl;

import cn.hutool.core.util.StrUtil;
import org.example.Ollama.client.api.OllamaApi;
import org.example.Ollama.client.pojo.OllamaChatOption;
import org.example.Ollama.client.pojo.OllamaChatRequest;
import org.example.Ollama.client.pojo.OllamaChatRespone;
import org.example.Ollama.client.pojo.OllamaMessage;
import org.example.Ollama.client.service.ollamaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import retrofit2.Call;
import retrofit2.Response;

import java.util.List;
@Service
public class ollamaServiceImpl implements ollamaService {
    @Autowired
    OllamaApi ollamaApi;
    @Override
    public String translate(String prompt) {
        try{
        if(StrUtil.isBlank(prompt)){
            throw new RuntimeException("prompt为空");
        }
        OllamaMessage ollamaMessage = new OllamaMessage();
        ollamaMessage.setRole("user");
        ollamaMessage.setContent("请将以下内容翻译为英文："+prompt);
        List<OllamaMessage> messages = List.of(ollamaMessage);
        OllamaChatRequest ollamaChatRequest = new OllamaChatRequest();
        ollamaChatRequest.setMessages(messages);
        ollamaChatRequest.setModel("qwen2.5:0.5b");
        Call<OllamaChatRespone> chat = ollamaApi.chat(ollamaChatRequest);
        if(chat==null){
            throw new RuntimeException("chat为空");
        }
            Response<OllamaChatRespone> execute = chat.execute();
            if(execute.isSuccessful()){
                System.out.println(execute.body());
                return execute.body().getMessage().getContent();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return prompt;//如果翻译失败就用中文，翻译成功就用英文
    }
}
